package com.juyu.miliao.common.util;


import com.juyu.miliao.common.constant.PageConstant;
import com.juyu.miliao.common.domain.BaseQuery;
import com.juyu.miliao.common.domain.PageIndex;

/**
 * @author jh
 * @date 2018/2/27 19:53
 */
public class PageInfoUtil {

    private PageInfoUtil() {
        throw new RuntimeException("工具类不允许创建实例");
    }

    /**
     * 降序
     */
    private static final String DESC = "desc";

    /**
     * 升序
     */
    private static final String ASC = "asc";

    /**
     * orderBy最大字符个数
     */
    private static final int ORDER_BY_MAX = 20;

    public static int getOffset(int pageNumber,int pageSize){
        return (pageNumber-1)*pageSize;
    }

    /**
     * 数据库手动分页
     * 注:Mysql使用分页limit偏移量越大性能影响严重,建议先缩小范围并用子查询走索引(不能用in)
     *
     * @param pageNum  页码
     * @param pageSize 每页个数
     * @param total    总个数
     * @param lastPage 超出后是否需要最后一页  true 需要
     */
    public static PageIndex getPageIndex(Integer pageNum, Integer pageSize, long total, boolean lastPage) {
        //初始化
        if (pageNum == null || pageNum <= 0) {
            pageNum = PageConstant.DEFAULT_PAGE_NUM;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = PageConstant.DEFAULT_PAGE_SIZE;
        }
        if (total < 0) {
            total = 0;
        }

        //起始页
        int offset;
        //总页数
        long pages = (long) Math.ceil((double) total / pageSize);

        if (lastPage) {
            if (pages > 0) {
                offset = (int) ((pages - 1) * pageSize);
            } else {
                offset = 0;
            }
        } else {
            if ((pageNum - 1) * pageSize >= total) {
                offset = PageConstant.OUT_OF_ROWS;
            } else {
                offset = (pageNum - 1) * pageSize;
            }
        }

        return new PageIndex(offset, pageSize, pageNum, pages, total);
    }

    /**
     * 数据库手动分页
     *
     * @param baseQuery baseQuery
     * @param total     total
     * @return PageIndex
     */
    public static PageIndex getPageIndex(BaseQuery baseQuery, long total, boolean lastPage) {
        cleanOrder(baseQuery);
        return getPageIndex(baseQuery.getPageNum(), baseQuery.getPageSize(), total, lastPage);
    }

    /**
     * 整理查询类query的排序,orderBy超过20个过滤,防止注入
     *
     * @param query query
     */
    public static void cleanOrder(BaseQuery query) {
        if (query != null) {
            String orderType = query.getOrderType();
            String orderBy = query.getOrderBy();
            if (CommonUtil.isEmptyString(orderBy) || orderBy.trim().length() > ORDER_BY_MAX) {
                query.setOrderType(null);
                query.setOrderBy(null);
            } else if (orderType == null) {
                //默认降序
                query.setOrderType(DESC);
            } else if (orderType.toLowerCase().equals(DESC)) {
                query.setOrderType(DESC);
            } else if (orderType.toLowerCase().equals(ASC)) {
                query.setOrderType(ASC);
            } else {
                query.setOrderType(DESC);
            }
        }
    }

}
